import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken } from '@/request/token'
Vue.use(VueRouter)

const routes = [
  { path: "/", component: () => import('@/views/Login') },
  {
    path: "/home", component: () => import('@/views/Home'),
    children:[
      { path: "/analysis", component: () => import('@/views/analysis') },
      { path: "/manage", component: () => import('@/views/manage') },
      { path: "/order", component: () => import('@/views/order') },
      { path: "/menu", component: () => import('@/views/menu') },
      { path: "/userDetail", component: () => import('@/views/userDetail') },
      { path: "/other", component: () => import('@/views/other') },
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, form, next) => {
  if (to.path == '/') {
    // 判断进入的是否是登录页 是就直接放行
    next()
    return
  }
  // 判断本地是否有token
  if (getToken()) {
    // 有放行
    next()
  } else {
    // 没有返回登录
    next('/')
  }
})
export default router
